<?php

/**
 * Created by PhpStorm.
 * User: linux
 * Date: 15/01/2018
 * Time: 12:26 PM
 */
namespace IoC;

class Container
{
    protected $binds = [];
    protected $instances = [];

    /**
     * 绑定
     * @param $abstract
     * @param $concrete
     */
    public function bind($abstract, $concrete)
    {
        if ($concrete instanceof \Closure) {
            $this->binds[$abstract] = $concrete;
        } else {
            $this->instances[$abstract] = $concrete;
        }
    }

    /**
     * 生产
     */
    public function make($abstract, $parameters = [])
    {
        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }
        array_unshift($parameters, $this);
        return call_user_func_array($this->binds[$abstract], $parameters);
    }
}